﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;
using System.Configuration;

namespace POSPlus.EZBarTender
{
    public class RegularNormalizer : LabelNormalizer
    {
        public RegularNormalizer()
            : base()
        {

        }

        public override void Normalize(DataTable table)
        {
            foreach (DataRow row in table.Rows)
            {
                DataRow informationRow = LabelInformation.NewRow();
                informationRow["Description"] = row["Description"];
                informationRow["Vendor"] = row["Vendor"];
                try
                {
                    informationRow["CircleFile"] = CircleFile(int.Parse(row["Vendor"].ToString()));
                }
                catch (FormatException ex)
                {
                    informationRow["CircleFile"] = ConfigurationManager.AppSettings["Blank"];
                    Console.WriteLine("CircleFile: " + row["Vendor"].ToString() + ", " + ex.Message);
                }
                informationRow["Cert_Code"] = row["Cert_Code"];
                try
                {
                    informationRow["AvgDailyMovement"] = AvgDailyMovement(double.Parse(row["AvgDailyMovement"].ToString()));
                }
                catch (FormatException ex)
                {
                    informationRow["AvgDailyMovement"] = "";
                    Console.WriteLine("AvgDailyMovement: " + row["AvgDailyMovement"].ToString() + ", " + ex.Message);
                }
                informationRow["Pack"] = row["Pack"];
                informationRow["Size"] = row["Size"];
                try
                {
                    informationRow["Unit_NP"] = Unit_NP(double.Parse(row["Unit_NP"].ToString()));
                }
                catch (FormatException ex)
                {
                    informationRow["Unit_NP"] = "";
                    Console.WriteLine("Unit_NP: " + ex.Message);
                }
                informationRow["UOM"] = row["UOM"];
                try
                {
                    informationRow["RegularUpcCheckbit"] =
                        RegularUpcCheckBit(
                        int.Parse(row["Vendor"].ToString()),
                        int.Parse(row["Cert_Code"].ToString()));
                }
                catch (FormatException ex)
                {
                    informationRow["RegularUpcCheckBit"] = "";
                    Console.WriteLine("RegualarUpcCheckBit: " + ex.Message);
                }
                try
                {
                    informationRow["RegularUpcNoCheckbit"] =
                        RegularUpcNoCheckBit(
                        int.Parse(row["Vendor"].ToString()),
                        int.Parse(row["Cert_Code"].ToString()));
                }
                catch (FormatException ex)
                {
                    informationRow["RegularUpcNoCheckbit"] = "";
                    Console.WriteLine("RegularUpcNoCheckbit: " + ex.Message);
                }
                try
                {
                    informationRow["Authorized"] = Authorized(int.Parse(row["Hosted"].ToString()));
                }
                catch (FormatException ex)
                {
                    informationRow["Authorized"] = "";
                    Console.WriteLine("Authorized: " + ex.Message);
                }
                try
                {
                    informationRow["RegularPriceLine1"] =
                        PriceLine1(
                            row["PriceMethod"].ToString(), row["Quantity"].ToString(), row["Normal_Price"].ToString(), row["GroupPrice"].ToString());
                }
                catch (FormatException ex)
                {
                    informationRow["RegularPriceLine1"] = "";
                    Console.WriteLine("RegularPriceLine1: " + ex.Message);
                }
                try
                {
                    informationRow["RegularPriceLine2"] =
                        PriceLine2(row["PriceMethod"].ToString(),row["Quantity"].ToString(),row["Normal_Price"].ToString(),row["GroupPrice"].ToString());
                }
                catch (FormatException ex)
                {
                    informationRow["RegularPriceLine2"] = "";
                    Console.WriteLine("RegularPriceLine2: " + ex.Message);
                }
                try
                {
                    informationRow["RegularPriceLine3"] =
                        PriceLine3(row["PriceMethod"].ToString(),
                            row["Quantity"].ToString(),
                            row["Normal_Price"].ToString(),
                            row["GroupPrice"].ToString());
                }
                catch (FormatException ex)
                {
                    informationRow["RegularPriceLine3"] = "";
                    Console.WriteLine("RegularPriceLine3: " + ex.Message);
                }

                informationRow["UPC"] = row["UPC"];
                informationRow["LocAisle"] = row["LocAisle"];
                informationRow["LocBlock"] = row["LocBlock"];
                informationRow["LocSeq"] = row["LocSeq"];
                informationRow["PrintDate"] = DateTime.Now.Date;
                informationRow["BatchNo"] = BatchNo(row["BatchNo"].ToString());
                LabelInformation.Rows.Add(informationRow);
                Console.WriteLine(LabelInformation.Rows[0]["Description"].ToString());
            }
        }

        public String BatchNo(string batchNo)
        {
            if (batchNo.ToLower().Contains("_ppc"))
            {
                return batchNo.Split('_')[0];
            }
            else return "";
        }
    }
}
